// 
// Copyright (c) 2006-2009 Ben Motmans
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Author(s):
//    Ben Motmans <ben.motmans@gmail.com>
//

using System;
using System.Collections.Generic;

namespace Anculus.Core
{
	/// <summary>
	/// Dispatch delegate for parameterless methods.
	/// </summary>
	public delegate void VoidDispatchHandler ();

	/// <summary>
	/// Dispatch delegate for methods with a single parameter.
	/// </summary>
	public delegate void ObjectDispatchHandler (object obj);

	/// <summary>
	/// A Dispatcher allows a method to be executed on another thread.
	/// Depending on the implementation, a method can be dispatched to the GUI thread or a background thread.
	/// 
	/// A dispatcher is more versatile then ThreadPool.QueueUserWorkItem, Control.Invoke, ... .
	/// It allows methods to be dispatched with a given delay, at a given interval, synchronous with the calling thread, etc.
	/// </summary>
	public interface IDispatcher
	{
		/// <summary>
		/// Initializes this <see cref="IDispatcher"/>.
		/// </summary>
		void Initialize ();

		/// <summary>
		/// Shuts this <see cref="IDispatcher"/> down.
		/// </summary>
		void Shutdown ();

		/// <summary>
		/// Dispatches the specified callback.
		/// </summary>
		/// <param name="callback">The callback.</param>
		/// <returns>The identifier of the dispatched callback, this can be used to cancel, reset, ... the callback</returns>
		ulong Dispatch (VoidDispatchHandler callback);

		/// <summary>
		/// Dispatches the specified callback.
		/// </summary>
		/// <param name="callback">The callback.</param>
		/// <param name="param">The param.</param>
		/// <returns>The identifier of the dispatched callback, this can be used to cancel, reset, ... the callback</returns>
		ulong Dispatch (ObjectDispatchHandler callback, object param);

		/// <summary>
		/// Dispatches the specified callback with a delay.
		/// </summary>
		/// <param name="delay">The delay (in milliseconds).</param>
		/// <param name="callback">The callback.</param>
		/// <returns>The identifier of the dispatched callback, this can be used to cancel, reset, ... the callback</returns>
		ulong DispatchWithDelay (int delay, VoidDispatchHandler callback);

		/// <summary>
		/// Dispatches the specified callback with a delay.
		/// </summary>
		/// <param name="delay">The delay (in milliseconds).</param>
		/// <param name="callback">The callback.</param>
		/// <param name="param">The param.</param>
		/// <returns>The identifier of the dispatched callback, this can be used to cancel, reset, ... the callback</returns>
		ulong DispatchWithDelay (int delay, ObjectDispatchHandler callback, object param);

		/// <summary>
		/// Dispatches the specified callback every <paramref name="interval"/> milliseconds.
		/// </summary>
		/// <param name="interval">The interval (in milliseconds).</param>
		/// <param name="callback">The callback.</param>
		/// <returns>The identifier of the dispatched callback, this can be used to cancel, reset, ... the callback</returns>
		ulong DispatchAtInterval (int interval, VoidDispatchHandler callback);

		/// <summary>
		/// Dispatches the specified callback every <paramref name="interval"/> milliseconds.
		/// </summary>
		/// <param name="interval">The interval (in milliseconds).</param>
		/// <param name="callback">The callback.</param>
		/// <param name="param">The param.</param>
		/// <returns>The identifier of the dispatched callback, this can be used to cancel, reset, ... the callback</returns>
		ulong DispatchAtInterval (int interval, ObjectDispatchHandler callback, object param);

		/// <summary>
		/// Dispatches the specified callback, this method will block the calling thread until the callback is executed.
		/// </summary>
		/// <param name="callback">The callback.</param>
		/// <returns></returns>
		ulong SyncDispatch (VoidDispatchHandler callback); //TODO: return void ?

		/// <summary>
		/// Dispatches the specified callback, this method will block the calling thread until the callback is executed.
		/// </summary>
		/// <param name="callback">The callback.</param>
		/// <param name="param">The param.</param>
		/// <returns></returns>
		ulong SyncDispatch (ObjectDispatchHandler callback, object param); //TODO: return void ?

		/// <summary>
		/// Dispatches the specified callback with a delay, this method will block the calling thread until the callback is executed.
		/// </summary>
		/// <param name="delay">The delay (in milliseconds).</param>
		/// <param name="callback">The callback.</param>
		/// <returns></returns>
		ulong SyncDispatchWithDelay (int delay, VoidDispatchHandler callback);

		/// <summary>
		/// Dispatches the specified callback with a delay, this method will block the calling thread until the callback is executed.
		/// </summary>
		/// <param name="delay">The delay (in milliseconds).</param>
		/// <param name="callback">The callback.</param>
		/// <param name="param">The param.</param>
		/// <returns>The identifier of the dispatched callback, this can be used to cancel, reset, ... the callback</returns>
		ulong SyncDispatchWithDelay (int delay, ObjectDispatchHandler callback, object param);

		/// <summary>
		/// Cancels the dispatched callback.
		/// </summary>
		/// <remarks>
		/// CancelDispatch removes the callback from the queue, this is useful to cancel a delayed callback.
		/// However, if the callback is currently executing, no action will be taken.
		/// </remarks>
		/// <param name="uid">The uid.</param>
		/// <returns><c>true</c> if the callback is cancelled; otherwise <c>false</c></returns>
		bool CancelDispatch (ulong uid);

		/// <summary>
		/// Resets the delay of the dispatched callback with the given identifier to the initial value.
		/// </summary>
		/// <param name="uid">The uid.</param>
		/// <returns><c>true</c> if the delay is reset; otherwise <c>false</c></returns>
		bool ResetDelay (ulong uid);

		/// <summary>
		/// Cancels the delay; in other words; execute the callback as soon as possible.
		/// </summary>
		/// <param name="uid">The uid.</param>
		/// <returns><c>true</c> if the delay is cancelled; otherwise <c>false</c></returns>
		bool CancelDelay (ulong uid);

		/// <summary>
		/// Changes the delay.
		/// </summary>
		/// <param name="uid">The uid.</param>
		/// <param name="delay">The delay.</param>
		/// <returns><c>true</c> if the delay is changed; otherwise <c>false</c></returns>
		bool ChangeDelay (ulong uid, int delay);

		/// <summary>
		/// Changes the interval.
		/// </summary>
		/// <param name="uid">The uid.</param>
		/// <param name="interval">The interval.</param>
		/// <returns><c>true</c> if the interval is changed; otherwise <c>false</c></returns>
		bool ChangeInterval (ulong uid, int interval);

		/// <summary>
		/// Determines whether thread dispatching is required.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if the calling thread is the same as the dispatch thread; otherwise <c>false</c>.
		/// </returns>
		bool IsDispatchRequired ();
	}
}
